﻿using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace WinRun
{
    class Alias
    {
        private string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\aliases.xml";

        #region Members

        public string AliasText
        {
            get;
            set;
        }

        public string Command
        {
            get;
            set;
        } 

        #endregion

        #region Constructors

        public Alias(string theAlias, string theCommand)
        {
            AliasText = theAlias;
            Command = theCommand;
        }
        public Alias()
        {

        } 

        #endregion

        #region Public

        public int Add(string theAlias, string theCommand)
        {
            if (AliasText.Length == 0 || Command.Length == 0)
            {
                return 1;
            }
            else
            {
                WriteAlias(theAlias, theCommand);
                return 0;
            }
        }

        public int Add()
        {
            if (AliasText.Length == 0 || Command.Length == 0 )
            {
                return 1;
            }
            else
            {
                WriteAlias(AliasText, Command);
                return 0;
            }
        }

        public int Remove(string theAlias)
        {
            if (AliasText.Length == 0)
            {
                return 1;
            }
            else
            {
                DeleteAlias(theAlias);
                return 0;
            }
        }

        public int Remove()
        {
            if (AliasText.Length == 0)
            {
                return 1;
            }
            else
            {
                DeleteAlias(AliasText);
                return 0;
            }
        }

        public void Change(string OriginalAlias, string theAlias, string theCommand)
        {
            /** Make sure the file exists **/
            if (File.Exists(filePath))
            {
                /** working with file in memory **/
                var xml = XElement.Load(filePath);

                var query = from p in xml.Elements("command")
                            where p.Attribute("alias").Value.ToString() == OriginalAlias
                            select p;

                foreach (var record in query)
                {
                    record.Remove();

                    /** write it out **/
                    var sw = new StreamWriter(filePath);
                    sw.Write(xml);
                    sw.Close();
                }

                WriteAlias(theAlias, theCommand);
            }
        }

        public string FindCommand(string theAlias)
        {
            var xml = XElement.Load(filePath);
            string retVal = string.Empty;
            var query = from p in xml.Elements("command")
                        where p.Attribute("alias").Value.ToString() == theAlias
                        select p;

            foreach (var record in query)
            {
                retVal = record.Value;
            }
            Command = retVal;
            return retVal;
        }

        #endregion

        #region Private

        private int WriteAlias(string theAlias, string theCommand)
        {
            /** Make sure the file exists **/
            if (File.Exists(filePath))
            {
                /** working with file in memory **/
                XmlReader reader = XmlReader.Create(filePath);
                XDocument xml = XDocument.Load(reader);
                reader.Close();

                /** create xml tree **/
                XElement wrk = xml.Descendants("aliases").Last();
                wrk.Add(new XElement("command", new XAttribute("alias", theAlias), new XElement("name", theCommand)));

                /** write it out **/
                StreamWriter sw = new StreamWriter(filePath);
                sw.Write("\n" + wrk + "\n");
                sw.Close();

            }

            return 0;
        }

        private int DeleteAlias(string theAlias)
        {
            /** Make sure the file exists **/
            if (File.Exists(filePath))
            {

                /** working with file in memory **/
                XElement xml = XElement.Load(filePath);

                var query = from p in xml.Elements("command")
                            where p.Attribute("alias").Value.ToString() == theAlias
                            select p;

                foreach (var record in query)
                {
                    record.Remove();

                    /** write it out **/
                    var sw = new StreamWriter(filePath);
                    sw.Write(xml);
                    sw.Close();
                }
                return 0;
            }
            return 1;
        }

        #endregion

        /// <summary>
        /// a cheesy way to create a new file until istall program
        /// </summary>
        public void CreateNewAliasFile()
        {
            var sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\aliases.xml", true);
            sw.WriteLine( @"<?xml version=""1.0"" encoding=""utf-8""?>");
            sw.WriteLine("<aliases>");
            sw.WriteLine(@"<command alias=""note""><name>notepad.exe</name></command>");
            sw.WriteLine(@"<command alias=""ie""><name>iexplore.exe</name></command>");
            sw.WriteLine(@"<command alias=""search""><name>www.google.com</name></command>");
            sw.WriteLine("</aliases>");
            sw.Flush();
            sw.Dispose();

        }
    }
}
